package v1

import (
	"RoccBlog/global"
	"RoccBlog/initalize"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	v1 "RoccBlog/repository/v1"
	"RoccBlog/service"
	"github.com/gin-gonic/gin"
)

// DataCacheService 数据库缓存业务层
type DataCacheService struct {
	service.BaseService
	//接口封装业务层CURD
	service.BaseOperationService
	//依赖的持久层
	Repository v1.DataCacheRepository
}

// 将DataCacheService注册到全局
func init() {
	global.RegisterService(&DataCacheService{
		Repository: v1.DataCacheRepository{},
	})
}

// Add 添加数据库缓存
func (s *DataCacheService) Add(ctx *gin.Context, data model.DataCache) error {
	err := s.Repository.Create(data)
	if err != nil {
		return err
	}
	err = initalize.RefreshDatabaseCache()
	return err
}

// Remove 删除数据库缓存
func (s *DataCacheService) Remove(ctx *gin.Context, id int) error {
	err := s.Repository.Delete(id)
	if err != nil {
		return err
	}
	err = initalize.RefreshDatabaseCache()
	return err
}

// Modify 修改数据库缓存
func (s *DataCacheService) Modify(ctx *gin.Context, data model.DataCache) error {
	err := s.Repository.Update(data)
	if err != nil {
		return err
	}
	err = initalize.RefreshDatabaseCache()
	return err
}

// FindById 查找ById数据库缓存
func (s *DataCacheService) FindById(ctx *gin.Context, id int) (model.DataCache, error) {
	return s.Repository.SelectById(id)
}

// Find 查找列表数据库缓存
func (s *DataCacheService) Find(ctx *gin.Context, params req_resp.DataCacheReq) (any, error) {
	size, num := s.GetPage(ctx)
	return s.Repository.Select(params, num, size)
}
